﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using BucaUIMvc.Repositories;

namespace BucaUIMvc
{
    public class LinkButtonExtension : ExtensionBase
    {
        private readonly LinkButtonSettings _settings;
        private readonly PermissionRepository _permissionRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="LinkButtonExtension"/> class.
        /// </summary>
        /// <param name="settings">The settings.</param>       
        public LinkButtonExtension(LinkButtonSettings settings)
        {
            _settings = settings;
            _permissionRepository = new PermissionRepository();
        }
        /// <summary>
        /// Gets the scripts.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override string GetScripts()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Gets the HTML.
        /// </summary>
        /// <returns></returns>
        public override MvcHtmlString GetHtml()
        {
            var path = HttpContext.Current.Request.RawUrl;            
            HttpContext.Current.Session["FA-Path"] = path;
            var controller = path.Remove(0, 1).Split('/')[0];
            HttpContext.Current.Session["Controller"] = controller;
            var perCode = _settings.Id;
           // var userId = _settings.UserId;
            var userId = 0;
            int.TryParse(HttpContext.Current.Session["FA-BigTime.UserId"].ToString(), out userId);
            var obj = _permissionRepository.CheckExistsInRoleInSite(path, perCode, userId);
            var tags = new StringBuilder();
            if (obj!=null)
            {
                var linkbutton = GetTopTag("a", _settings.CssClass, _settings);
                linkbutton.MergeAttribute("href", _settings.Href + "/" + perCode);
                linkbutton.SetInnerText(obj.Title);
                for (var i = 0; i < _settings.Attributes.Length ;i+=2)
                {
                    linkbutton.Attributes.Add(new KeyValuePair<string, string>(_settings.Attributes[i].ToString(), _settings.Attributes[i+1].ToString()));
                }
                tags.Append(linkbutton);
            }
            return MvcHtmlString.Create(tags.ToString());
        }        
    }

    public class LinkButtonSettings : SettingsBase
    {
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>
        /// The text.
        /// </value>
        public object[] Attributes { get; set; }
        /// <summary>
        /// Gets or sets the href.
        /// </summary>
        /// <value>
        /// The href.
        /// </value>
        public string Href { get; set; }
        /// <summary>
        /// Gets or sets the style.
        /// </summary>
        /// <value>
        /// The style.
        /// </value>
        public string Style { get; set; }
        /// <summary>
        /// Gets or sets the CSS class.
        /// </summary>
        /// <value>
        /// The CSS class.
        /// </value>
        public string CssClass { get; set; }
    }
}
